EEM Applet and TCL scrips (Count & email )

Below 2 email will be sent at 23:00. One will contain the contains of file RTPReport.txt and the second the contains of SIPcount.txt

A snapshot of the number of rtp streams is taken every 30 minute.
A counter of the number of SIP redirect message is recorded every 30 min and reset to 0. In the example below i am using EIGRP Hello messages for testing.

event manager environment _email_server 10.1.1.1
event manager environment _email_to user@email.com
event manager environment _email_from Router-Hostname
event manager environment countSIP30min 5
event manager directory user policy “flash:/”

event manager applet CaptureSIP_Event
event syslog pattern “HELLO”
action 1010 set tmp30 “$countSIP30min”
action 1020 increment tmp30
action 1030 cli command “enable”
action 1040 cli command “config t”
action 1050 cli command “event manager environment countSIP30min $tmp30”
action 1060 cli command “exit”

event manager applet emailFile
event timer cron cron-entry “00 23 * * *”
action 1010 cli command “enable”
action 1020 cli command “more flash:SIPcount.txt”
action 1030 mail server “$_email_server” to “$_email_to” from “$_email_to” subject “SIP Redirect Count for Day” body “File output below\n========\n$_cli_result\n”
action 1040 cli command “delete /force flash:SIPcount.txt”
action 1050 cli command “more flash:RTPReport.txt”
action 1060 mail server “$_email_server” to “$_email_to” from “$_email_to” subject “RTP Stream Count for Day” body “File output below\n========\n$_cli_result\n”
action 1070 cli command “delete /force flash:RTPReport.txt”

TCL scripts
===========
event manager policy sipcount.tcl
event manager policy rtpstreams.tcl

rtpstreams.tcl script

::cisco::eem::event_register_timer watchdog time 1800
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
#open an exec session.
#catch return 1 if error occured
#result contains error or result of excuting script
if { [catch {cli_open} result] } {
error $result $errorInfo
}
#saved results to array var $cli
array set cli $result
#one of the elemets in $cli array: fd=CLI channel handler
if { [catch {cli_exec $cli(fd) “enable”} result] } {
error $result $errorInfo
}
if { [catch {cli_exec $cli(fd) “show clock”} result] } {
error $result $errorInfo
}
set listClockData [split $result “\n”]
if { [catch {cli_exec $cli(fd) “show ip int brief | include 10.1.1|192.2.2”} result] } {
error $result $errorInfo
}
set listRtpData [split $result “\n”]
set intNoRtp [llength $listRtpData]
incr intNoRtp -1
set strLine “[string trim [lindex $listClockData 2]]\t[string trim [lindex $listClockData 1]]\t$intNoRtp”
#puts “Line:$strLine”
#append to file
set fd [open “flash:RTPReport.txt” a]
puts $fd $strLine
close $fd
#close the cli session
catch {cli_close $cli(fd) $cli(tty_id)}

sipcount.tcl script

::cisco::eem::event_register_timer watchdog time 1800
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
if {![info exists countSIP30min]} {
set result “Policy cannot be run: variable countSIP30min dose not exist”
error $result $errorInfo
}
#open an exec session.
#catch return 1 if error occured
#result contains error or result of excuting script
if { [catch {cli_open} result] } {
error $result $errorInfo
}
#saved results to array var $cli
array set cli $result

#one of the elemets in $cli array: fd=CLI channel handler
if { [catch {cli_exec $cli(fd) “enable”} result] } {
error $result $errorInfo
}
#get time
if { [catch {cli_exec $cli(fd) “show clock”} result] } {
error $result $errorInfo
}
set listClockData [split $result “\n”]
#get the last 30 min sip redirect count
if { [catch {cli_exec $cli(fd) “sh event manager environment countSIP30min”} result] } {
error $result $errorInfo
}
set listSIPData [split $result “\n”]
#reset cpunt to 0 for next 30 min
if { [catch {cli_exec $cli(fd) “configure terminal”} result] } {
error $result $errorInfo
}
if { [catch {cli_exec $cli(fd) “event manager environment countSIP30min 0”} result] } {
error $result $errorInfo
}
if { [catch {cli_exec $cli(fd) “end”} result] } {
error $result $errorInfo
}
#format string to write
set strLine “[string trim [lindex $listClockData 2]]\t[string trim [lindex $listClockData 1]]\t[string trim [lindex $listSIPData 1]]”
#write result to file
append to file
set fd [open “flash:SIPcount.txt” a]
puts $fd $strLine
close $fd
#close the cli session
catch {cli_close $cli(fd) $cli(tty_id)}

Posted in EEM